package com.lamb.script.runner;

import com.lamb.script.core.executor.ScriptEngineCallable;
import com.lamb.script.core.utils.ScriptUtil;

import java.net.URL;

/**
 * @author : lamb
 * @version : 1.0
 * @date :  2019-06-04 16:41
 **/
public class ScriptMain {

    public static void main(String[] args) {
        String file = args.length > 0 ? args[0] : null;
        if (null == file) {
            System.out.println("no js file.");
            return;
        }
        // 可支持 tools 下的工具类.
        String path = "classpath:/tools/" + file + "Tool.js";
        URL fileEveryWhere = ScriptUtil.getFileEveryWhere(path);
        if (null != fileEveryWhere) {
            file = path;
        }
        String finalFile = file;
        ScriptEngineCallable scriptEngineCallable = new ScriptEngineCallable(scriptName -> finalFile);
        try {
            scriptEngineCallable.set("args", args);
            scriptEngineCallable.call();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
